What about HasUserCode in Enterprise Core Objects – MDriven Framework

Class Attributes in classes modeled with ECO have a property called HasUserCode. This post explains what effect the property has and its intended use.

Let us say we have this model:

Hasusercode - 1.png

Focus on the DueDate attribute and look at the Object inspector:

Hasusercode - 2.png

By default, the HasUserCode property is false – this says that there are NO side effects or checks applied in code that influence or stop anyone from having the value set.

Let us generate the code:

Hasusercode - 3.png

The Invoice class is implemented as a partial class where Invoice.cs is yours and Invoice.eco.cs is maintained by the Framework.

Open up the Framework-owned Invoice.eco.cs and look at the attribute definition:

Code Snippet

[UmlElement(Id="256adf06-35be-47e7-9189-e882c4bb779f", Index=Eco_LoopbackIndices.DueDate)]
public System.DateTime DueDate {
get {
return ((System.DateTime)(this.eco_Content.get_MemberByIndex(Eco_LoopbackIndices.DueDate)));
}
set {
this.eco_Content.set_MemberByIndex(Eco_LoopbackIndices.DueDate, ((object)(value)));
}
}

As you can see, there is no room for extravaganza while setting or reading the attribute “DueDate”.

Let us set HasUserCode to true for the DueDate attribute in the model. Update the code and look at the Framework-owned file again:

Code Snippet

[UmlElement(Id="256adf06-35be-47e7-9189-e882c4bb779f", Index=Eco_LoopbackIndices.DueDate)]
[UmlTaggedValue("Eco.HasUserCode", "True")]
public System.DateTime DueDate {
get {
this.DueDateReading();
System.DateTime res = ((System.DateTime)(this.eco_Content.get_MemberByIndex(Eco_LoopbackIndices.DueDate)));
this.DueDateRead(ref res);
return res;
}
set {
System.DateTime oldValue = this.DueDate;
System.DateTime newValue = value;
bool abortModification = false;
this.DueDateChanging(ref value, ref abortModification);
if (abortModification) {
return;
}
this.eco_Content.set_MemberByIndex(Eco_LoopbackIndices.DueDate, ((object)(value)));
this.DueDateChanged(oldValue, newValue, value);
}
}

Notice the difference. We now have calls to DueDateReading, DueDateRead, DueDateChanging, and ateChanged.

These are all Partial Methods declared by the Framework. Partial Method is a .net language construct that is perfect for Frameworks like ECO to use. ECO added these definitions to the Invoice class:

Code Snippet

#region *** DueDate partial methods ***

/// <summary>This method is called before the attribute DueDate is read</summary>
partial void DueDateReading();

/// <summary>This method is called after DueDate is read. It is possible to change the value here.</summary>
partial void DueDateRead(ref DateTime value);

/// <summary>This method is called before DueDate is modified, it is possible to change the value that is stored here or abort the modification</summary>
partial void DueDateChanging(ref DateTime value, ref bool abortModification);

/// <summary>This method is called after DueDate is modified</summary>
/// <param name="oldValue">This is the value that DueDate had before the modification</param>
/// <param name="newValue">This is the value that someone tried to set</param>
/// <param name="finalValue">This is the value that DueDate has after the modification</param>
partial void DueDateChanged(DateTime oldValue, DateTime newValue, DateTime finalValue);

#endregion *** DueDate partial methods ***

As the developer, you need only one or two of these interception points – but ECO gave you all. Unused Partial Methods are stripped by the compiler, however, and cost you nothing in runtime.

Now we can head back to Invoice.cs – the implementation file that you use and which the Framework does not touch:

public partial class Invoice {
}

Very empty so far, but we can now implement one of the interception points. Just write “partial” and see what comes up:

Hasusercode - 4.png

Pick the one you want and add your stuff:

Code Snippet

partial void DueDateChanging(ref DateTime value, ref bool abortModification)
{
if (value < this.Date.AddDays(5))
abortModification = true;
}

Why Not Do This All the Time?

The typical question after explaining what HasUserCode is would be: “Why would you ever want to turn HasUserCode off?“ It is a relevant question since the Partial Methods do not cost anything in runtime – we might as well leave the HasUserCode on all the time...

The answer is slightly more complex. ECO has the ambition to be as effective and efficient as possible. As a result, the internal state of objects is not stored in the frontend object but in a centralized cache. This strategy gives us advantages on many levels – like holding object identities before they are fully loaded, knowing what we have loaded or not, signaling changes, ensuring association ends even if not completely loaded, executing models without generated code, etc.

When following expressions for subscriptions – created automatically or explicitly, or expressions from ViewModels, Handles, Constraints, or Actions – ECO does not need to route the call through the frontend object. It can head straight for the cache and cut a few CPU cycles from the workload – which might not be much but since Eco is a Framework, these things add up and become important.

UNLESS the read or write attribute has the HasUserCode flag set – if it has ECO, it will always route access through the frontside object.

Does this mean that HasUserCode==true makes my code slower? Technically yes – it will cost you a couple of CPU cycles. So, still “a lot slower?” No! You should not worry about that if you need the interception points, but you should leave HasUserCode set to false if you have no intention of using the interception points.

This page was edited 107 days ago on 01/11/2024. What links here